package com.rykj.energy.database.service.impl;

import com.rykj.energy.common.utils.CollectionUtils;
import com.rykj.energy.common.utils.StringUtils;
import com.rykj.energy.database.mapper.CameraPresettingMapper;
import com.rykj.energy.database.pojo.BasicCameraPresetting;
import com.rykj.energy.database.service.ICameraPresettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("cameraPresettingService")
public class CameraPresettingServiceImpl implements ICameraPresettingService {

    @Autowired
    private CameraPresettingMapper cameraPresetMapper;

    @Override
    public List<BasicCameraPresetting> getPresetsByDevId(Integer devId){
       return cameraPresetMapper.getPresetsByDevId(devId);
    }

    @Override
    public void saveCameraPreset(BasicCameraPresetting cameraPreset) {
            cameraPresetMapper.insert(cameraPreset);
    }

    @Override
    public void updateCameraPreset(BasicCameraPresetting cameraPreset) {
        cameraPresetMapper.updateById(cameraPreset);
    }

    @Override
    public List<BasicCameraPresetting> getAllPresets() {
        return cameraPresetMapper.getAllPresets();
    }

    @Override
    public void addOrUpdatePreset(Integer cameraId ,List<Integer> presetIds ,List<BasicCameraPresetting> cameraPresets) {
        if(CollectionUtils.isNotEmpty(presetIds)){
            cameraPresetMapper.deleteNotInIds(cameraId,presetIds);
        }

        for (BasicCameraPresetting cameraPreset:cameraPresets) {
            if(StringUtils.isEmpty(cameraPreset.getId())||cameraPreset.getId()==0){
                cameraPresetMapper.insert(cameraPreset);
            }else {
                cameraPresetMapper.updateById(cameraPreset);
            }
        }
    }

    @Override
    public BasicCameraPresetting findById(Integer presetId) {
        return cameraPresetMapper.selectById(presetId);
    }
}
